<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改文章信息')" />
	<th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-article-edit" th:object="${info}">
            <input id="articleId" name="articleId" th:field="*{articleId}"  type="hidden">
            <div class="form-group">
                <label class="col-sm-2 control-label">标题：</label>
                <div class="col-sm-10">
                    <input id="articleTitle" th:field="*{articleTitle}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2 control-label">选择图片：</div>
                <div class="col-sm-10">
                    <div class="input-group">
                        <input id='imgName' th:value="${info?.imgName}"  class="form-control" onclick="$('#i-file').click();">
                        <label class="input-group-btn">
                            <input type="button" id="i-check" value="浏览图片" class="btn btn-primary" onclick="$('#i-file').click()">
                        </label>
                    </div>
                </div>
                <input type="hidden" name="imgPath" th:value="${info?.imgPath}">
                <input type="hidden" name="imgName" th:value="${info?.imgName}">
                <input type="file" name="file" id='i-file'  accept=".jpg, .png, .jpeg, " onchange="uploadImage(this.files[0]);" style="display: none">
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">所属栏目：</label>
                <div class="col-sm-10">
                    <select name="columnId" class="form-control m-b" >
                        <option th:each="item : ${columnInfoList}" th:selected="${item.columnId == info.columnId}" th:text="${item.columnName}" th:value="${item.columnId}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">简介：</label>
                <div class="col-sm-10">
                    <textarea  maxlength="500" th:field="*{articleAbstract}" class="form-control" rows="3"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">内容：</label>
                <div class="col-sm-10">
                    <input id="articleContent"  th:field="*{articleContent}" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
		</form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">
        var prefix ="/sys/article";
    
	    $(function() {
		    $('.summernote').summernote({
		    	placeholder: '请输入文章内容',
		    	height : 192,
				lang : 'zh-CN',
				followingToolbar: false,
				callbacks: {
	                onImageUpload: function (files) {
	                    sendFile(files[0], this);
	                }
	            }
		    });
			var content = $("#articleContent").val();
		    $('#editor').summernote('code', content);
	    });
	    
	    // 上传文件
	    function sendFile(file, obj) {
	        var data = new FormData();
	        data.append("file", file);
	        $.ajax({
	            type: "POST",
	            url: "/common/upload",
	            data: data,
	            cache: false,
	            contentType: false,
	            processData: false,
	            dataType: 'json',
	            success: function(result) {
	                if (result.state == web_status.SUCCESS) {
	                	$(obj).summernote('editor.insertImage', result.data.url, result.data.fileName);
					} else {
						$.modal.alertError(result.msg);
					}
	            },
	            error: function(error) {
	                $.modal.alertWarning("图片上传失败。");
	            }
	        });
	    }
        //上传图片
        function uploadImage(file){
            $("#imgName").val(file.name);
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: "/common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(data) {
                    if (data.state == web_status.SUCCESS) {
                        $("input[name='imgPath']").val(data.data.url);
                        $("input[name='imgName']").val(data.data.fileName);
                    } else {
                        $.modal.alertError(data.data.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }

		$("#form-article-edit").validate({
			focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	var sHTML = $('.summernote').summernote('code');
				$("#articleContent").val(sHTML);
				$.operate.save(prefix + "/saveOrUpdate", $('#form-article-edit').serialize());
	        }
	    }
	</script>
</body>
</html>
